package com.yndsoft.jc.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yndsoft.jc.model.ViewHolder;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.KeyValue;

import java.util.List;

/**
 * Created by Administrator on 2016-08-05.
 */
public class KeyValueAdapter extends BaseAdapter {
    // 上下文
    private Context context;
    // 填充数据的list
    private List<KeyValue> list;

    // 构造器
    public KeyValueAdapter(Context context, List<KeyValue> list) {
        super();
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater layoutInflater = LayoutInflater.from(context);// //创建视图容器工厂并设置上下文
        ViewHolder listView = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.key_value_item, null); // 创建list_item.xml布局文件的视图
            listView = new ViewHolder();
            listView.id = (TextView) convertView.findViewById(R.id.key_value_id);
            listView.name = (TextView) convertView.findViewById(R.id.key_value_name);
            convertView.setTag(listView);
        } else {
            listView = (ViewHolder) convertView.getTag();
        }
        listView.id.setText(String.valueOf(list.get(position).getKey()));
        listView.name.setText(list.get(position).getValue());
        return convertView;
    }
}
